const {port,host} = require("./config")
const Koa = require("koa");
const app = new Koa();

//body解析
const bodyparser = require("koa-bodyparser")
app.use(bodyparser())

//路由环境(批量注册路由器)
/*const userRouter = require("./router/user")
const orderRouter = require("./router/order")
app.use(userRouter.routes()).use(userRouter.allowedMethods())
app.use(orderRouter.routes()).use(orderRouter.allowedMethods())*/
const Router = require("@koa/router")
const requireDirectory = require('require-directory');
requireDirectory(module,"./router" ,{visit: (obj)=>{
    if(obj instanceof Router){
        app.use(obj.routes()).use(obj.allowedMethods())
    }
}});

//错误处理
const error = require("koa-json-error")
const _ = require("lodash")
app.use(error({
    postFormat: (e, obj) =>
        process.env.NODE_ENV === 'pro' ? _.omit(obj, 'stack') : obj
}))

//参数检验
const  parameter = require("koa-parameter");
parameter(app)

//连接数据库
require("./db/db.js")

app.listen(port,host)